package com.activititest.controller;


import com.activititest.appentity.*;
import com.activititest.entity.*;
import com.activititest.mapper.VTaskCmdFormMapper;
import com.activititest.service.*;

import com.activititest.service.impl.VTaskCommitPulltantServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.*;

//
///**
// * <p>
// *  带确认列表
// * </p>
// *
// * @author mfx
// * @since 2020-06-01
// */
@RestController
@RequestMapping("/activititest/taskcmdform")
public class VTaskCmdFormController {
    @Autowired
    private IVTaskCmdFormService vTaskCmdFormService;
    @Autowired
    private OrdinaryUsersServer ordinaryUsersServer;
    @Autowired
    private IEmTaskCmdFormService emTaskCmdFormService;
    @Autowired
    private IEmEnterpriseService iEmEnterpriseService;
    @Autowired
    private IVTaskCmdFormService iVTaskCmdFormService;
    @Autowired
    private IVTaskCmdPulltantService iVTaskCmdPulltantService;
    @Autowired
    private IEmTaskCmdPolltantService iEmTaskCmdPolltantService;
    @Autowired
    private HistoryService historyService;
    @Autowired
    private IVTaskCommitPulltantService iVTaskCommitPulltantService;
    @Autowired
    private IVTaskCommitFromService iVTaskCommitFromService;

//    //查询待确认任务
    @PostMapping("getconfirmlist/")
    public Response<List<Statistics_content_list>> getConfirmList(@RequestParam("areaid")String areaid){
        List<String> taskids = ordinaryUsersServer.findTasks(areaid);
        List<Statistics_content_list> tatistics_content_lists=new ArrayList<>();
        if(!taskids.isEmpty()){
            QueryWrapper<VTaskCmdForm> vTaskCmdFormqw=new QueryWrapper<VTaskCmdForm>();
            vTaskCmdFormqw.in("task_id",taskids);
            List<VTaskCmdForm> vTaskCmdForms = vTaskCmdFormService.list(vTaskCmdFormqw);

            Statistics_content_list statistics_content_list;
            for(VTaskCmdForm taskCmdForm:vTaskCmdForms){
                statistics_content_list = new Statistics_content_list();
                statistics_content_list.setTaskid(taskCmdForm.getTaskId());
                statistics_content_list.setEnterprise_name(taskCmdForm.getEnterpriseName());
                statistics_content_list.setTaxk_date(taskCmdForm.getCmdFormDatetime());
                tatistics_content_lists.add(statistics_content_list);
            }
        }
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(tatistics_content_lists);
        euserResponse.setMsg("yes");
        return euserResponse;
    }

//    //确认
    @PostMapping("confirmtask/")
    public String confirmTask(@RequestParam("areaid")String areaid,@RequestParam("taskid")String taskid){
        String s = ordinaryUsersServer.QueryTasks(areaid, taskid);
        UpdateWrapper<EmTaskCmdForm> queryWrapper = new UpdateWrapper<EmTaskCmdForm>();
        queryWrapper.eq("task_id", taskid);
        EmTaskCmdForm emTaskCmdForm = new EmTaskCmdForm();
        emTaskCmdForm.setTaskId(s);
        emTaskCmdFormService.update(emTaskCmdForm, queryWrapper);

        UpdateWrapper<EmTaskCmdPolltant> emTaskCmdPolltantWrapper = new UpdateWrapper<EmTaskCmdPolltant>();
        emTaskCmdPolltantWrapper.eq("task_id", taskid);
        EmTaskCmdPolltant emTaskCmdPolltant = new EmTaskCmdPolltant();
        emTaskCmdPolltant.setTaskId(s);
        iEmTaskCmdPolltantService.update(emTaskCmdPolltant, emTaskCmdPolltantWrapper);
        return s;
    }


//过去待确定列表
    @PostMapping("getsentlist/")
    public Response<List<Statistics_content_list>> getSentList(@RequestParam("areaid")String areaid){
        ListidAndTime sendTasks = ordinaryUsersServer.findSendTasks(areaid);
        List<Statistics_content_list> tatistics_content_lists=new ArrayList<>();
        if (!sendTasks.getIds().isEmpty()){
            List<VTaskCmdForm> vTaskCmdForms = vTaskCmdFormService.list(new QueryWrapper<VTaskCmdForm>().in("task_id", sendTasks.getIds()));
            List<Date> times = sendTasks.getTimes();
            Statistics_content_list statistics_content_list;
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for(VTaskCmdForm taskCmdForm:vTaskCmdForms){
                statistics_content_list = new Statistics_content_list();
                statistics_content_list.setTaskid(taskCmdForm.getTaskId());
                statistics_content_list.setEnterprise_name(taskCmdForm.getEnterpriseName());
                statistics_content_list.setTaxk_date(taskCmdForm.getCmdFormDatetime());
                for (int i = 0;i<sendTasks.getIds().size();i++){
                    if (sendTasks.getIds().get(i).equals(taskCmdForm.getTaskId())){
                        statistics_content_list.setTaxk_confirm_date(formatter.format(times.get(i)));
                        break;
                    }
                }
                tatistics_content_lists.add(statistics_content_list);
            }
        }
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(tatistics_content_lists);
        euserResponse.setMsg("yes");
        return euserResponse;
    }
    //获取待确认任务详情
    @PostMapping("getsentinfor/")
    public Response<List<EmEnterpriseInfor>> getSentInfor(@RequestParam("taskid")String taskid){
        System.out.println(taskid);
        EmTaskCmdForm emTaskCmdForm = emTaskCmdFormService.getOne(new QueryWrapper<EmTaskCmdForm>().eq("task_id",taskid));
        EmEnterprise vEmEnterprisepoll = iEmEnterpriseService.getById(emTaskCmdForm.getEnterpriseId());
        Map<String, Object> hashMap = new HashMap() {{
            put("task_id", taskid);
        }};
        List<VTaskCmdPulltant> vTaskCmdPulltants = iVTaskCmdPulltantService.listByMap(hashMap);
//        List<EmTaskCmdPolltant> emTaskCmdPolltants = iEmTaskCmdPolltantService.listByMap(hashMap);
        EmEnterpriseInfor emEnterpriseInfor = new EmEnterpriseInfor();

        emEnterpriseInfor.setEnterprise_address(vEmEnterprisepoll.getEnterpriseAddress());
        emEnterpriseInfor.setEnterprise_id(String.valueOf(vEmEnterprisepoll.getEnterpriseId()));
        emEnterpriseInfor.setEnterprise_latitude(vEmEnterprisepoll.getEnterpriseLatitude());
        emEnterpriseInfor.setEnterprise_longitude(vEmEnterprisepoll.getEnterpriseLongitude());
        emEnterpriseInfor.setEnterprise_name(vEmEnterprisepoll.getEnterpriseName());
        List<EmPollutant> eps=new ArrayList();
        EmPollutant emPollutant;
        for(VTaskCmdPulltant vTaskCmdPulltant:vTaskCmdPulltants){
            emPollutant = new EmPollutant(vTaskCmdPulltant.getPollutantId(),vTaskCmdPulltant.getPollutantType(),vTaskCmdPulltant.getPollutantName());
            System.out.println(emPollutant.getPollutant_id());
            eps.add(emPollutant);
        }
        emEnterpriseInfor.setEmPollutants(eps);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(emEnterpriseInfor);
        euserResponse.setMsg("yes");
        return euserResponse;
    }
    //已完成数据
    @PostMapping("finshtask/")
    public Response<List<Statistics_content_list>> finshTask(@RequestParam("areaid")String areaid){
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );

        List<String> pids=new ArrayList();
        List<Statistics_content_list> Statistics_content_lists=new ArrayList();
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().taskCandidateGroup(areaid).taskName("runsend").finished().list();
        Statistics_content_list statistics_content_list;
        for(HistoricTaskInstance instance:list){
            pids.add(instance.getProcessInstanceId());
            statistics_content_list=new Statistics_content_list();
            statistics_content_list.setTaxk_complete_date(sdf.format(instance.getEndTime()));
            statistics_content_list.setTaskid(instance.getProcessInstanceId());
            Statistics_content_lists.add(statistics_content_list);
        }

        QueryWrapper<VTaskCommitFrom> qw = new QueryWrapper<VTaskCommitFrom>().in("task_id", pids);
        List<Statistics_content_list> Statistics_content_listsw=new ArrayList();
        List<VTaskCommitFrom> list1 = iVTaskCommitFromService.list(qw);
        for(VTaskCommitFrom vTaskCommitPulltant:list1){
            for(Statistics_content_list ss:Statistics_content_lists){
                if(ss.getTaskid().equals(vTaskCommitPulltant.getTaskId())){
                    statistics_content_list=new Statistics_content_list();
                    statistics_content_list.setTaxk_date( ss.getTaxk_date());
                    statistics_content_list.setTaxk_complete_date(ss.getTaxk_complete_date());
                    statistics_content_list.setTaskid(ss.getTaskid());
                    statistics_content_list.setEnterprise_name(vTaskCommitPulltant.getEnterpriseName());
                    Statistics_content_listsw.add(statistics_content_list);
                }
            }
        }
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(Statistics_content_listsw);
        euserResponse.setMsg("yes");
        return euserResponse;

    }
    @PostMapping("finshtaskinfor/")
    public Response<EmEnterpriseInfor> finshTaskinfor(@RequestParam("taskid")String taskid){
        SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
        QueryWrapper<VTaskCommitPulltant> qw = new QueryWrapper<VTaskCommitPulltant>().eq("task_id", taskid);
        List<VTaskCommitPulltant> list1 = iVTaskCommitPulltantService.list(qw);
        System.out.println(list1.size());
        EmEnterpriseInfor emEnterpriseInfor = new EmEnterpriseInfor();
        System.out.println("++++++++++================="+taskid+"-=-=-=-=");
        HistoricTaskInstance sendTask = historyService.createHistoricTaskInstanceQuery().processInstanceId(taskid).taskName("send").finished().singleResult();
        HistoricTaskInstance confirmTask = historyService.createHistoricTaskInstanceQuery().processInstanceId(taskid).taskName("confirm").finished().singleResult();
        HistoricTaskInstance runsendTask = historyService.createHistoricTaskInstanceQuery().processInstanceId(taskid).taskName("runsend").finished().singleResult();
        if(list1.size()>0){
            VTaskCommitPulltant vTaskCommitPulltant = list1.get(0);
            emEnterpriseInfor.setEnterprise_name(vTaskCommitPulltant.getEnterpriseName());
            emEnterpriseInfor.setEnterprise_longitude(vTaskCommitPulltant.getEnterpriseLongitude());
            emEnterpriseInfor.setEnterprise_latitude(vTaskCommitPulltant.getEnterpriseLatitude());
            emEnterpriseInfor.setEnterprise_address(vTaskCommitPulltant.getEnterpriseAddress());

            emEnterpriseInfor.setTask_date(sdf.format(sendTask.getEndTime()));
            emEnterpriseInfor.setTask_confirm_date(sdf.format(confirmTask.getEndTime()));
            emEnterpriseInfor.setTask_complete(sdf.format(runsendTask.getEndTime()));

            List<EmPollutant> eps=new ArrayList();
            EmPollutant emPollutant;
            for(VTaskCommitPulltant vt:list1){
                emPollutant=new EmPollutant(vt.getPollutantId(),vt.getPollutantType(),vt.getPollutantName(),vt.getPollutantValue());
                eps.add(emPollutant);
            }
            emEnterpriseInfor.setEmPollutants(eps);
        }
        System.out.println(emEnterpriseInfor);
        Response euserResponse = new Response();
        euserResponse.setCode(200);
        euserResponse.setData(emEnterpriseInfor);
        euserResponse.setMsg("yes");
        return euserResponse;

    }
}

